const express = require('express')
const c = require('../connection')
const router = express.Router()
//修改员工信息
router.post('/update', (req, res) => {
    var obj = req.body
    c.query(`update uc_member set ? where mid=?`, [obj, obj.mid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
})
//删除员工
router.post('/delete',(req,res)=>{
    var obj=req.body
    c.query(`delete from uc_member where mid=?`, [obj.mid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.affectedRows === 1) {
            res.send({
                code: 200,
                msg: '删除成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '删除失败',
                data: r
            })
        }
    })
})
//新增员工
router.post('/add', (req, res) => {
    var obj = req.body
    c.query(`insert into uc_member set ?`, [obj], (err, r) => {
        if (err) {
            throw err
        }
        res.send({
            code: 200,
            msg: '新增员工信息成功',
            data: r
        })
    })
})
//部门列表
router.get('/department', (req, res) => {
    c.query(`select * from uc_department`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '部门查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '部门查询失败',
            })
        }
    })
})
//员工列表
router.get('/list', (req, res) => {
    c.query(`select * from uc_member`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '员工列表查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '员工列表查询失败',
            })
        }
    })
})
router.get('/query',(req,res)=>{
    var obj=req.query
    c.query(`select * from uc_member where name like ?`,[`%${obj.name}%`],(err,r)=>{
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '员工信息查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '员工信息查询失败',
            })
        }
    })
})
module.exports = router
